﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace hw5
{
    internal class Homework
    {
        public static void Main()
        {
            Homework demo = new Homework();
            demo.workOne();
            demo.workTwo();
        }

        public bool isPrime(int n)
        {
            //两个较小数另外处理
            if (n == 2 || n == 3)
                return true;
            //不在6的倍数两侧的一定不是质数
            if (n % 6 != 1 && n % 6 != 5)
                return false;
            int tmp = (int)Math.Sqrt(n);
            //在6的倍数两侧的也可能不是质数
            for (int i = 5; i <= tmp; i += 6)
                if (n % i == 0 || n % (i + 2) == 0)
                    return false;
            //排除所有，剩余的是质数
            return true;
        }

        public void workOne()
        {
            int count = 1;
            Console.WriteLine("6-100的素数之和的表示");
            for (int i = 6; i < 100; i++)
            {
                for (int start = 3; start <= i / 2; start++)
                {
                    if (isPrime(start) && isPrime(i - start))
                    {
                        Console.Write("{0}={1}+{2}; \t", i, start, (i - start));
                        count++;
                    }
                    if (count == 8)
                    {
                        Console.WriteLine();
                        count = 1;
                    }
                }

            }
            Console.WriteLine();
        }

        /*struct num
        {
            public int id;
            public int value;
        }*/

        public void workTwo()
        {
            Console.WriteLine("0-1000的一百个数字的操作");
            Random random = new Random();
            int[] arr = new int[100];
            for (int i = 0; i < 100; i++)
            {
                arr[i] = random.Next(0, 1001);
            }

            IEnumerable<int> query = from num in arr
                                     where 1 == 1
                                     orderby num descending
                                     select num;

            int[] array = query.ToArray();
            int sum = query.Sum(num => num);
            double aver = query.Average(num => num);

            int count = 1;
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i] + "\t");
                count++;
                if (count == 10)
                {
                    Console.WriteLine();
                    count = 1;
                }
            }
            Console.WriteLine();
            Console.WriteLine("这100个数字的和是{0}",sum);
            Console.WriteLine("这100个数字的平均数是{0}",aver);
        }
    }
}
